/**
 *  Copyright 2010 ShenYang voole Co. Ltd.
 *  All right reserved. 
 */
package com.dianning.dao.hibernate;


import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.*;

import com.dianning.bean.Channel;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.dao.VisitorDAO;

/**
 * 功能描述:访客操作实现
 *
 * 创建日期 Apr 10, 2010
 *
 * @author zhangbaochao
 *
 * @version 1.0
 *
 */
public class VisitorHibernateDAO extends HibernateDaoSupport implements VisitorDAO {

	private static final String LOAD_BY_ID = "from Visitor where id = ?";
	private static final String LOAD_BY_IP_UID = "from Visitor where ip = ? and uid = ?";
	private static final String GET_NUM_BY_UID = "select count(*) from Visitor where CONVERT(varchar(12),lastTime,111)=CONVERT(varchar(12),getdate(),111) and uid = ?";
	
	/**
	 * 保存Visitor对象
	 * 
	 * @param v
	 * 			Visitor对象
	 * @return Visitor
	 * 			Visitor对象
	 */
	public Visitor saveVisitor(Visitor visitor){
		this.getHibernateTemplate().save(visitor);
		return visitor;
	}
	/**
	 * 保存或更新Visitor对象
	 * 
	 * @param visitor
	 * 			Visitor对象
	 * @return Visitor
	 * 			Visitor对象
	 */
	public Visitor saveOrUpdateVisitor(Visitor visitor){
		this.getHibernateTemplate().saveOrUpdate(visitor);
		return visitor;
	}
	/**
	 * 保存Visitor对象
	 * 
	 * @param visitor
	 * 			Visitor对象
	 * @return Visitor
	 * 			Visitor对象
	 */
	public Visitor updateVisitor(Visitor visitor){
		this.getHibernateTemplate().update(visitor);
		return visitor;
	}
	
	/**
	 * 根据主键ID获取Visitor对象
	 * 
	 * @param id
	 * 			主键ID
	 * @return Visitor
	 * 			Visitor对象
	 */
	public Visitor findVisitorById(long id){
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,id);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Visitor) l.get(0);
		}
	}
	
	/**
	 * 要据IP uid 获取Visitor对象
	 * 
	 * @param ip
	 * 			String
	 * @param uid
	 * 			long
	 * @return Visitor
	 * 			Visitor对象
	 */
	public Visitor findVisitorByIpUid(String ip,long uid){
		Object[] o = {ip, new Long(uid)};
	    List l = this.getHibernateTemplate().find(LOAD_BY_IP_UID, o);
	    if (l == null || l.isEmpty()) {
	      return null;
	    }
	    else {
	      return (Visitor) l.get(0);
	    }
	}
	
	/**
	 * 获取今天Visitor个数
	 * @return long
	 * 			Visitor个数
	 */
	public long getTodayVisitorNumByUid(long uid){
		List l = getHibernateTemplate().find(GET_NUM_BY_UID, new Long(uid));
		if (l == null || l.isEmpty()) {
	      return 0;
	    }
	    else {
	      return ((Long) l.get(0)).longValue();
	    }
	}
	
	/**
	 * 获取Visitor个数
	 * 
	 * @return int
	 * 			Visitor个数
	 */
	public long getVisitorNum(){
		List l = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(UserInfo.class);
				c.setProjection(Projections.count("id"));
				return c.list();
			}
		});
		if (l == null || l.isEmpty()) {
			return 0;
		} else {
			return ((Long) l.get(0)).longValue();
		}
	}
	
	/**
	 * 获取Visitor列表
	 * 
	 * @param orderby
	 *            String
	 * @param ascOrDesc
	 *            int
	 * @param firstResult
	 *            int
	 * @param maxResults
	 *            int
	 * @return List
	 * 			Visitor对象列表
	 */
	public List findVisitors(final String orderby, final int ascOrDesc, final int firstResult, final int maxResults){
		return getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException {
				Criteria c = s.createCriteria(Channel.class);
				if (StringUtils.isNotBlank(orderby)) {
					if (ascOrDesc == Constant.ORDER_ASC) {
						c.addOrder(Order.asc(orderby));
					}
					if (ascOrDesc == Constant.ORDER_DESC) {
						c.addOrder(Order.desc(orderby));
					}
				}
				c.setFirstResult(firstResult);
				c.setMaxResults(maxResults);
				return c.list();
			}
		});
	}
}
